<?php

namespace backend\models;

use Yii;

/**
 * This is the model class for table "{{%sms}}".
 *
 * @property integer $id
 * @property string $mobile
 * @property integer $sms_code
 * @property integer $type
 * @property integer $created_at
 */
class Sms extends \yii\db\ActiveRecord
{
    /**
     * @inheritdoc
     */
    public static function tableName()
    {
        return '{{%sms}}';
    }

    /**
     * @inheritdoc
     */
    public function rules()
    {
        return [
            ['created_at', 'filter', 'filter' => function () {
                return $this->created_at ? strtotime($this->created_at) : time();
            }],
            [['mobile', 'sms_code', 'created_at'], 'required'],
            [['sms_code', 'type', 'created_at'], 'integer'],
            [['mobile'], 'string', 'max' => 11],
        ];
    }

    /**
     * @inheritdoc
     */
    public function attributeLabels()
    {
        return [
            'id' => '自增ID',
            'mobile' => '手机号',
            'sms_code' => '手机验证码',
            'type' => '类型:1注册2登录',
            'created_at' => '创建时间',
        ];
    }

    /**
     * Finds mobile by mobile and sms_code
     *
     * @param string $mobile
     * @param int $sms_code
     * @return static|null
     */
    public static function findByMobileSmsCode($mobile, $sms_code)
    {
        return static::findOne(['mobile' => $mobile, 'sms_code' => $sms_code, 'created_at' => ['>=', time() - 10 * 60]]);
    }
}
